/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.model;

import java.io.IOException;
import java.util.StringTokenizer;

import soccerscope.file.LogFileReader;

public class ServerSetMaker extends Thread {

	private SceneBuilder builder;

	private SceneSet sceneSet;

	private LogFileReader lfr;

	public ServerSetMaker(SceneBuilder builder, SceneSet sceneSet) {
		this.builder = builder;
		this.sceneSet = sceneSet;
		this.lfr = (LogFileReader) builder;
	}

	@Override
	public void run() {
		try {
			setName("DAI-NAMITE SoccerScope ServerSetMarker");
		} catch (SecurityException e) {
			System.out.println("Could not set the thread name");
		}
		// ClientScene clientScene = this.sceneSet.lastScene();
		// Team left = clientScene.left;
		// Team right = clientScene.right;
		// PlayMode playmode = clientScene.pmode;
		String packet = new String();
		// System.out.println("going to read packet");
		while (true) {
			try {
				packet = this.lfr.readServerLog();
				if (packet == null) {
					System.out.println("done");
					this.sceneSet.analyze();
					// Toolkit.getDefaultToolkit().beep();
					this.lfr.closeServerLog();
					return;
				}
			} catch (IOException ie) {
				System.err.println(ie);
				return;
			}
			StringTokenizer st = new StringTokenizer(packet);
			if (st.hasMoreTokens()) {
				int time = Integer.parseInt(st.nextToken());
				if (time > 0) {
					String send = new String();
					while (st.hasMoreTokens()) {
						send = send + st.nextToken() + " ";
					}
					this.builder.addMessage((ClientScene) this.sceneSet
							.getScene(time), send);
				}
			}
		}
	}
}
